package com.iwinding.ladygg.ui.act;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.iwinding.ladygg.R;
import com.iwinding.ladygg.net.RequestManager;
import com.iwinding.ladygg.net.callback.BaseRpCallback;
import com.iwinding.ladygg.net.request.FindpwdReset;
import com.iwinding.ladygg.net.response.BaseRopnse;
import com.iwinding.ladygg.utils.ToastUtil;

/**
 * Created by hezhiyong on 2017/3/31.
 */

public class ForgetPwSetActivity extends BaseActivity implements View.OnClickListener {

    public static final String CAPTCHA = "captcha";

    private AutoCompleteTextView mEtPw;
    private AutoCompleteTextView mEtPwConfirm;

    private ImageView mImgCleanPw;
    private ImageView mImgCleanPwConfirm;

    private Button mBtnConfrim;

    private String captcha = "";

    @Override
    protected int getLayoutID() {
        return R.layout.icane_activity_forget_pwset;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setBackTxtResId(R.string.icane_forget_password);
        setTitleResId(R.string.icane_forget_password);

        captcha = getIntent().getStringExtra(CAPTCHA);

        initView();
        setListener();
    }

    void initView() {

        mEtPw = (AutoCompleteTextView) findViewById(R.id.icane_edit_pw);
        mEtPwConfirm = (AutoCompleteTextView) findViewById(R.id.icane_edit_pw_confirm);

        mImgCleanPw = (ImageView) findViewById(R.id.clear_pw);
        mImgCleanPwConfirm = (ImageView) findViewById(R.id.clear_pw_confirm);

        mBtnConfrim = (Button) findViewById(R.id.icane_confirm);


    }

    void setListener() {
        mEtPw.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                mImgCleanPw.setVisibility(s.length() == 0 ? View.GONE : View.VISIBLE);
            }
        });
        mEtPwConfirm.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                mImgCleanPwConfirm.setVisibility(s.length() == 0 ? View.GONE : View.VISIBLE);
            }
        });

        mImgCleanPw.setOnClickListener(this);
        mImgCleanPwConfirm.setOnClickListener(this);

        mBtnConfrim.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.clear_phone:
                mEtPw.setText("");
                break;
            case R.id.clear_vercode:
                mEtPwConfirm.setText("");
                break;
            case R.id.icane_confirm:
                confrim();
                break;
        }
    }


    /**
     * 确定
     */
    void confrim() {
        final FindpwdReset findpwd = new FindpwdReset();
        String pw = mEtPw.getEditableText().toString().trim();
        String pwConfirm = mEtPwConfirm.getEditableText().toString().trim();
        if (TextUtils.isEmpty(pw)) {
            ToastUtil.openEmptyPw(this);
            return;
        }
        if (TextUtils.isEmpty(pwConfirm)) {
            ToastUtil.openEmptyPwConfirm(this);
            return;
        }
        if (!TextUtils.equals(pw,pwConfirm)) {
            ToastUtil.openEmptyPwDif(this);
            return;
        }
        findpwd.newPwd = pwConfirm;
        findpwd.captcha = captcha;

        RequestManager.postFindpwdReset(mContext,findpwd, new BaseRpCallback() {

            @Override
            public Object parseResponse(BaseRopnse response, int id) {
                return response;
            }

            @Override
            public void onResult(Object o) {
                ToastUtil.openSetPWSuccess(ForgetPwSetActivity.this);
                finish();
            }

            @Override
            public void onFail(String errorMsg) {
                ToastUtil.openBaseToast(ForgetPwSetActivity.this,errorMsg,Toast.LENGTH_SHORT);
            }
        });
    }

}
